using System;
using System.Collections.Generic;
using System.Data;
using Orchard.ContentManagement.Drivers;
using Orchard.ContentManagement.MetaData;
using Orchard.ContentManagement.MetaData.Builders;
using Orchard.Core.Contents.Extensions;
using Orchard.Data.Migration;
using SchoolPointe.RelatedContent.Enums;
using SchoolPointe.RelatedContent.Extensions;


namespace SchoolPointe.Menus {
    public class Migrations : DataMigrationImpl {

        public int Create() {
            //several of these properties already exist on the built in menu/menu item parts, but I want to avoid excessive joins
			SchemaBuilder.CreateTable("BetterMenuItemPartRecord", table => table
				.ContentPartRecord()
				.Column("Text", DbType.String)
                .Column("SubTitle", DbType.String)
                .Column("Url", DbType.String)
                .Column("MenuItemType", DbType.Int32)
                .Column("SortOrder", DbType.Int32)
			);

			SchemaBuilder.CreateTable("BetterMenuPartRecord", table => table
				.ContentPartRecord()
				.Column("Name", DbType.String)
			);

            ContentDefinitionManager.AlterTypeDefinition("Menu",
                cfg => cfg
                    .WithPart("CommonPart")
                    .WithPart("BetterMenuPart")
                    .WithPart("RelatedContentPart", config => config.WithContentRelationship("Children", "menu-items", RelationType.HasMany, false, new string[] { "BetterMenuItem" }, inverseOf: "MenuItem.menuitem-parent"))
                    .WithPart("IdentityPart")
                    .Creatable()
                );

            ContentDefinitionManager.AlterTypeDefinition("MenuItem",
                cfg => cfg
                    .DisplayedAs("Menu Item")
                    .WithPart("CommonPart")
                    .WithPart("BetterMenuItemPart")
                    .WithPart("RelatedContentPart", config => config
                        .WithContentRelationship("Menu Content", "menu-content", RelationType.HasOne, false, null, "RelatedContentRelationshipViewModel-Hidden")
                        .WithContentRelationship("Parent", "menuitem-parent", RelationType.HasOne, true, new string[] { "MenuItem, Menu" }, "RelatedContentRelationshipViewModel-Hidden")
                        .WithContentRelationship("Children", "menu-items", RelationType.HasMany, false, new string[] { "MenuItem" }, "RelatedContentRelationshipViewModel-Hidden", "MenuItem.menuitem-parent")
                    )
                    .WithPart("IdentityPart")
                );

            ContentDefinitionManager.AlterTypeDefinition("MenuWidget",
                cfg => cfg
                    .WithPart("CommonPart")
                    .WithPart("WidgetPart")
                    .WithPart("RelatedContentPart", config => config
                        .WithContentRelationship("Menu", "menu", RelationType.HasOne, true, new string[] { "Menu" }, "RelatedContentRelationshipViewModel")
                        )
                    .WithPart("IdentityPart")
                    .WithSetting("Stereotype", "Widget")
                );

            return 3;
        }

        public int UpdateFrom1()
        {
            ContentDefinitionManager.AlterTypeDefinition("Menu", cfg => cfg.WithPart("IdentityPart"));
            ContentDefinitionManager.AlterTypeDefinition("MenuItem", cfg => cfg.WithPart("IdentityPart"));            
            return 2;
        }

        public int UpdateFrom2()
        {
            ContentDefinitionManager.AlterTypeDefinition("MenuWidget", cfg => cfg.WithPart("IdentityPart"));
            return 3;
        }
    }
}